<?php

namespace App\Livewire\Admin\User;

use App\Models\User;
use Livewire\Attributes\Layout;
use Livewire\Component;

#[Layout('components.admin.layout')]
class Update extends Component
{
    public $name;
    public $email;
    public User $user;

    public function mount(User $user) {
        $this->user =  $user;
        $this->fill($user->only('name', 'email'));
    }

    public function store()
    {

        $this->user->name = $this->name;
        $this->user->email = $this->email;
        $res = $this->user->save();
        // TODO 添加成功后要在页面加入该条数据
        if ($res) {
            $html = view('admin.user.list-item', ['user' => User::find($this->user['id'])])->render();
            $this->dispatch('update-row', [
                'html' => $html,
                'id'  => $this->user['id'],
            ]);
            $this->dispatch('close-modal');
        }
    }

    public function render()
    {
        return view('livewire.admin.user.update');
    }
}
